package com.itheima.quartz;


import com.itheima.aliyunoss.AliyunUtils;
import com.itheima.pub.RedisConstant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Set;

@Component
public class AliyunOssScanClean {

    @Autowired
    RedisTemplate redisTemplate;

    @Scheduled(cron = "*/10 * * * * ?")  // 声明任务
    public void cleanJunkImg(){
        // 此方法为获取与第一个参数集合比较,第二个参数集合中没有的值的set集合
        Set difference = redisTemplate.opsForSet().difference(RedisConstant.SETMEAL_PIC_RESOURCES, RedisConstant.SETMEAL_PIC_DB_RESOURCES);

        // 为确保此程序正常运行,做个长度判断
        if (difference.size()>0){
            for (Object o : difference) {
                System.out.println((String) o);
                AliyunUtils.deleteFile((String) o);
            }
            // 删除redis上两个集合
            redisTemplate.delete(RedisConstant.SETMEAL_PIC_DB_RESOURCES);
            redisTemplate.delete(RedisConstant.SETMEAL_PIC_RESOURCES);
        }
        System.out.println("---quartz--is-running---");
    }
}
